package com.xinyu.web.testJdbcTemplate;

import java.beans.PropertyVetoException;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.xinyu.web.entity.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationcontext.xml")
public class TestJDBC {
	@Resource(name="userDao")
	private UserDao ud;
	
	@Test
	public void fn() throws PropertyVetoException {
		//准备连接池
		ComboPooledDataSource DataSource = new ComboPooledDataSource();
		DataSource.setDriverClass("com.mysql.jdbc.Driver");
		DataSource.setJdbcUrl("jdbc:mysql:///mydb");
		DataSource.setUser("root");
		DataSource.setPassword("root");
		
		//创建JDBC模板对象
		JdbcTemplate jt = new JdbcTemplate();
		jt.setDataSource(DataSource);
		
		//书写SQL语句并执行
		String sqlString="insert into t_user values(null,'tom')";
		jt.update(sqlString);
	}
	
	@Test
	public void fn1() throws PropertyVetoException {
		User u = new User();
		u.setName("alic");
		ud.save(u);
	}
	@Test
	public void fn2() throws PropertyVetoException {
		ud.del(2);
	}
	@Test
	public void fn3() throws PropertyVetoException {
		User u = new User();
		u.setId(3);
		u.setName("jack");
		ud.update(u);
	}
	
	@Test
	public void fun5() throws Exception{
		System.out.println(ud.getTotalCount());
	}
	
	@Test
	public void fun6() throws Exception{
		System.out.println(ud.getByID(1));
	}
	
	@Test
	public void fun7() throws Exception{
		System.out.println(ud.getAll());
	}
}
